﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InventoryMgtServiceRef;
    using WMS6.Web.UserControls;

    public class TransferList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnNewSearch;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected HtmlForm F5062;
        protected GridView gvTransfer;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblTransfer;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindDataTransferList(TransferQueryEntity transferQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(transferQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = InventoryMgtServiceClientProxy.GetInstance().GetInvTransferInfoByQueryEntity(transferQuery);
            base.BindData2GridView(this.gvTransfer, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            TransferQueryEntity transferQuery = this.GetTransferQueryEntity();
            transferQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = transferQuery.CurrentPage;
            transferQuery.CurrentPage = "0";
            this.gvTransfer.AllowSorting = false;
            this.BindDataTransferList(transferQuery);
            this.hdnActionID.Value = "";
            this.gvTransfer.ShowFooter = false;
            transferQuery.IsGetAll = false;
            transferQuery.CurrentPage = currentPageIndex;
            this.SetTransferQueryEntity(transferQuery);
            base.Export2Excel("Transfer.xls", this.gvTransfer);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            TransferQueryEntity transferQuery = this.GetTransferQueryEntity();
            transferQuery.CurrentPage = "0";
            this.BindDataTransferList(transferQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            TransferQueryEntity transferQuery = this.GetTransferQueryEntity();
            transferQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindDataTransferList(transferQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            TransferQueryEntity transferQuery = this.GetTransferQueryEntity();
            if (int.Parse(transferQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                transferQuery.CurrentPage = (int.Parse(transferQuery.CurrentPage) + 1).ToString();
                this.BindDataTransferList(transferQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            TransferQueryEntity transferQuery = this.GetTransferQueryEntity();
            if (int.Parse(transferQuery.CurrentPage) > 0)
            {
                transferQuery.CurrentPage = (int.Parse(transferQuery.CurrentPage) - 1).ToString();
                this.BindDataTransferList(transferQuery);
            }
        }

        private TransferQueryEntity GetTransferQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as TransferQueryEntity);
            }
            return (base.PageQueryEntitySession as TransferQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                TransferQueryEntity transferQuery = this.GetTransferQueryEntity();
                transferQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindDataTransferList(transferQuery);
            }
        }

        protected void gvTransfer_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5062", this.gvTransfer);
        }

        protected void gvTransfer_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvTransfer_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                TransferQueryEntity TransferQuery = this.GetTransferQueryEntity();
                TransferQuery.SortField = e.SortExpression;
                if (TransferQuery.SortDirection == "ASC")
                {
                    TransferQuery.SortDirection = "DESC";
                }
                else if (TransferQuery.SortDirection == "DESC")
                {
                    TransferQuery.SortDirection = "ASC";
                }
                this.BindDataTransferList(TransferQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            TransferQueryEntity transferQuery = new TransferQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    transferQuery.TransferID = this.PreviousPage.TransferID;
                    transferQuery.Status = this.PreviousPage.Status;
                    transferQuery.TransferType = this.PreviousPage.TransferType;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            transferQuery.IsGetAll = false;
            transferQuery.CurrentPage = "0";
            transferQuery.PageSize = this.gvTransfer.PageSize.ToString();
            transferQuery.SortField = "TRANSFER_ID";
            transferQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvTransfer.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetTransferQueryEntity(transferQuery);
            this.BindDataTransferList(transferQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetTransferQueryEntity(TransferQueryEntity transferQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = transferQuery;
            }
            else
            {
                base.PageQueryEntitySession = transferQuery;
            }
        }

        public TransferQuery PreviousPage
        {
            get
            {
                return (TransferQuery) base.PreviousPage;
            }
        }
    }
}

